﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class admin_contact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            BindContactInfo();
            BindNotice();
        }
        private void BindContactInfo()
        {
            List<ContactInfo> list = ContactInfoLogic.GetContactInfo();
            if (list == null)
                return;
            ContactInfo contact = ContactInfoLogic.GetContactInfo().FirstOrDefault();
            txbCname.Text = contact.CName;
            txbEname.Text = contact.EName;
            txbCAdress.Text = contact.CAdress;
            txbEAdress.Text = contact.EAdress;
            txbECopyRight.Text = contact.ECopyRight;
            txbCCopyRight.Text = contact.CCopyRight;
            txbBeiAn.Text = contact.BeiAn;
            txbTelphone.Text = contact.Telephone;
            txbEmailAddress.Text = contact.EmailAdress;
        }
        private void BindNotice()
        {
            noticefail.Visible = false;
            noticesuccess.Visible = false;
        }
        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            ContactInfo contact = createContactInfo();
            int isSuccess = ContactInfoLogic.UpdateContactInfo(contact);
            if(isSuccess != 1){
                ChangeStatus(false);
            }else{
                ChangeStatus(true);
            }

        }
        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefail.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();", true);
        }
        private ContactInfo createContactInfo()
        {
            ContactInfo contact = new ContactInfo();
            contact.CName = txbCname.Text.Replace("'","''");
            contact.EName = txbEname.Text.Replace("'", "''");
            contact.CAdress = txbCAdress.Text.Replace("'", "''");
            contact.EAdress = txbEAdress.Text.Replace("'", "''");
            contact.ECopyRight = txbECopyRight.Text.Replace("'", "''");
            contact.CCopyRight = txbCCopyRight.Text.Replace("'", "''");
            contact.BeiAn = txbBeiAn.Text.Replace("'", "''");
            contact.Telephone = txbTelphone.Text.Replace("'", "''");
            contact.EmailAdress = txbEmailAddress.Text.Replace("'", "''");
            return contact;
        }
    }
}
